<template>
  <div class="uporder">
    <div class="uporder_main">
      <div class="card_top">
        <el-card :body-style="{ padding: '10' }" style="text-align: left;">
          <div class="taber">
            <div style="margin-left: 10px;">请校验商品</div>
            <el-input
              placeholder="请输入商品链接进行检验!"
              v-model="goodsurl"
              clearable
              class="goodsurl"
            >
            </el-input>
            <div>
              <el-button
                style="width:100%;margin-left: 10px;"
                type="primary"
                @click="goaddgoods"
                >校验商品</el-button
              >
            </div>
          </div>
          <div style="margin-left:18px">
            <div class="info">
              <p style="color:red">重要提醒:</p>
              <p>1、已支持团长链接，填写信息时可补充!</p>
              <p>2、务必确保信息准确无误再提交，任何一项错误，直接拒绝!</p>
              <p>3、请确保已经和商家对接清楚，切勿中途调整价格和佣金!</p>
              <p>4、若中途商品有变化，请及时在线修改或者下架商品!</p>
              <p style="color:red">
                5、按要求提交朋友圈文案和有特殊活动类型的商品，可提升综合权重，增加曝光!
              </p>
            </div>
          </div>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script>
// import { setflag } from "@/utils/auth";
import { checkGoodsTesting } from "@/api/upgoods/upgoods";
export default {
  name: "teamset",
  data() {
    return {
      goodsurl: "",
      id: "",
      info: {
        itemId: "", //商品id
        title: "", //商品标题
        images: [], //主图数组
        rootCategoryId: "", //分类原始id
        shopName: "", //店铺名称
        goodRatePercentage: "", //店铺好评率
        priceMoney: "", //商品原价
        evaluates: [] //店铺评分数组
      }
    };
  },

  computed: {},
  created() {
    // this.goaddgoods();
  },
  mounted() {},
  methods: {
    goaddgoods() {
      // console.log(this.goodsurl);
      // this.goodsurl =
      //   "https://detail.m.tmall.com/item.htm?id=606540358182&ali_trackid=2:mm_40370965_22602780_74958456:";
      console.log(this.goodsurl);
      if (this.goodsurl === "") {
        this.$message.info("请输入商品地址!");
      } else {
        var reg2 = /(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|&|-)+)/g; // 筛选出url g全局 i
        // var reg = /(https?|http|ftp|file):\/\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]/g;

        var str = this.goodsurl;
        var arr = str.match(reg2); // 筛选出符合格式的数组 arr2=>["url","url"]
        // console.log(arr);
        // const urlstr = arr[0].substring(0, arr[0].length - 14);
        // console.log("urlstr", urlstr);

        var reg = /id=(\d+)/i;
        this.goodsurl.match(reg);
        const idArr = this.goodsurl.match(reg);
        console.log("商品id", idArr[1]);
        this.id = idArr[1];
        this.$jsonp(
          "https://acs.m.taobao.com/h5/mtop.taobao.detail.getdetail/6.0/?&api=mtop.taobao.detail.getdetail&v=6.0&ttid=2013%40taobao_h5_1.0.0&type=jsonp&dataType=jsonp&data=%7B%22itemNumId%22%3A%22" +
            this.id +
            // "590515928833" +
            "%22%7D"
        ).then(res => {
          this.id = "";
          console.log("淘宝返回", res);
          this.$ls.set("seller", res.data.seller);
          let priceobj = {};
          priceobj = JSON.parse(res.data.apiStack[0].value);
          console.log("apiStack.value", priceobj);
          console.log("priceText", priceobj.price.price.priceText);
          if (priceobj.price.price.priceMoney) {
            this.info.priceMoney = priceobj.price.price.priceMoney / 100;
          } else {
            const pricestr = priceobj.price.price.priceText;
            this.info.priceMoney = pricestr.split("-")[0]; //最低价
            // this.info.priceMoney = pricestr.split("-")[1];//最高价
          }
          console.log("this.info.priceMoney", this.info.priceMoney);
          this.info.itemId = res.data.item.itemId;
          this.info.title = res.data.item.title;
          this.info.images = res.data.item.images;
          this.info.rootCategoryId = res.data.item.rootCategoryId;
          this.info.shopName = res.data.seller.shopName;
          this.info.goodRatePercentage = res.data.seller.goodRatePercentage;
          this.info.evaluates = res.data.seller.evaluates;
          this.$ls.set("tbinfo", this.info);
          console.log(this.$ls.get("tbinfo"));
          checkGoodsTesting({
            item_id: this.info.itemId,
            shop_name: this.info.title,
            root_category_id: this.info.rootCategoryId
          })
            .then(res => {
              console.log("商品信息", res);
              this.$ls.set("goodsinfo", res.data);
              this.$router.push({
                path: "/userinfo/addgoods",
                query: {
                  // url: urlstr,
                  url: arr[0]
                  // data: data
                }
              });
            })
            .catch(err => {
              console.log(err);
            });
        });
      }
    }
  }
};
</script>

<style lang="less" scoped>
.uporder {
  // width: 970px;
  // height: 500px;
  box-sizing: border-box;
  .uporder_main::v-deep {
    // width: 970px;
    .taber {
      width: 500px;
      margin: 0 auto;
      .goodsurl {
        width: 500px;
        margin-left: 10px;
        margin-bottom: 10px;

        .el-input__inner {
          // font-weight: bold;
          height: 47px;
          box-sizing: border-box;
          background-color: #f7f8fa;
          border: 2px solid rgb(224, 224, 224);
        }
        .el-input__inner:hover {
          border: 2px solid #6183ff;
        }
      }
    }
    .info {
      width: 500px;
      margin: 25px auto;
      // padding-right: 10px;
      box-sizing: border-box;
      padding: 15px;
      background-color: #eff3f7;
    }
  }
}
</style>
